package com.xqzgj.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xqzgj.common.annotation.Log;
import com.xqzgj.common.core.controller.BaseController;
import com.xqzgj.common.core.domain.AjaxResult;
import com.xqzgj.common.enums.BusinessType;
import com.xqzgj.system.domain.StockRecord;
import com.xqzgj.system.service.IStockRecordService;
import com.xqzgj.common.utils.poi.ExcelUtil;
import com.xqzgj.common.core.page.TableDataInfo;

/**
 * 库存记录Controller
 * 
 * @author xqzgj
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/system/record")
public class StockRecordController extends BaseController {
    @Autowired
    private IStockRecordService stockRecordService;

    /**
     * 查询库存记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockRecord stockRecord) {
        startPage();
        List<StockRecord> list = stockRecordService.selectStockRecordList(stockRecord);
        return getDataTable(list);
    }

    /**
     * 导出库存记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:record:export')")
    @Log(title = "库存记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, StockRecord stockRecord) {
        List<StockRecord> list = stockRecordService.selectStockRecordList(stockRecord);
        ExcelUtil<StockRecord> util = new ExcelUtil<StockRecord>(StockRecord.class);
        util.exportExcel(response, list, "库存记录数据");
    }

    /**
     * 获取库存记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stockRecordService.selectStockRecordById(id));
    }

    /**
     * 新增库存记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "库存记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StockRecord stockRecord) {
        return toAjax(stockRecordService.insertStockRecord(stockRecord));
    }

    /**
     * 修改库存记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "库存记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockRecord stockRecord) {
        return toAjax(stockRecordService.updateStockRecord(stockRecord));
    }

    /**
     * 删除库存记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "库存记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(stockRecordService.deleteStockRecordByIds(ids));
    }

    /**
     * 审批库存记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:approve')")
    @Log(title = "库存记录", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approve(@RequestBody StockRecord stockRecord) {
        return toAjax(stockRecordService.approveStockRecord(stockRecord));
    }

    /**
     * 取消审批库存记录
     */
    @PreAuthorize("@ss.hasPermi('system:record:cancel')")
    @Log(title = "库存记录", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody StockRecord stockRecord) {
        return toAjax(stockRecordService.cancelStockRecord(stockRecord));
    }
}